package com.hhit.collaborative_learning.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.hhit.collaborative_learning.dao.BBS_SpecialDao;

public class BBS_SpecialDaoImpl implements BBS_SpecialDao {

	@Override
	public ResultSet queryBBS_Special(Connection con, int start, int end,
			String findwhate, String findby, String order, String sort) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM `collaborative_learning`.`bbs_special` ";
		if (findwhate != null && findby != null) {
			sql += " where " + findwhate + " " + findby + " ";
		}
		if (sort != null) {
			sql += " order by " + sort + " ";
			if (order != null) {
				sql += " " + order;
			}
		}
		if (end != 0 && start < end) {
			sql += " limit " + start + "," + end;
		} 
		PreparedStatement prs;
		try {
			prs = con.prepareStatement(sql);
			return prs.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("非法请求：" + sql);
			return null;
		}

	}

	@Override
	public int delete(Connection con, String find) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM `collaborative_learning`.`bbs_special` WHERE "+find+" ";
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			System.out.println("非法sql语句  "+sql);
			return 0;
		} 
	}

	@Override
	public int insert(Connection con, String title) {
		// TODO Auto-generated method stub
		String sql="INSERT INTO `collaborative_learning`.`bbs_special`(`Name`,`CreatedOn`)VALUES(?,?);";
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			prs.setString(1, title);
			prs.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			return 0;
		} 
	}

}
